<template>
  <el-container>
    <el-header>
      <div class="homeLogo">
        <img src="../assets/heima.png" alt />
        <span>电商管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <el-container>
      <el-aside :width="iscollapse?'64px': '200px'">
        <div class="toggle_button" @click="btnToggle">|||</div>
        <el-menu
          default-active="2"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          :collapse="iscollapse"
          :collapse-transition="false"
          :unique-opened="true"
          :router="true"
          :default-active='activePath'
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.path+''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
            >
              <!-- @click="saveNavstate('/'+subItem.path)" -->
              <!-- 二级菜单模板 -->
              <template>
                <i class="el-icon-menu"></i>
                <span slot="title">{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
        <!-- 右侧布局 -->
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconObj: {
        "125": "iconfont icon-user",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },
      iscollapse: false,
      activePath: ""
    };
  },
  created() {
    this.getmenuList();
    // this.activePath=window.sessionStorage.getItem('activePath')
    // 刷新页面获取路由地址
    this.activePath = location.hash.slice(1);
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    async getmenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
      console.log(res);
    },
    btnToggle() {
      this.iscollapse = !this.iscollapse;
    }
    // saveNavstate(activePath){
    //   window.sessionStorage.setItem('activePath',activePath)
    //   this.activePath=activePath
    // },
  },
  watch: {
    $route(to, from) {
      this.activePath = to.path;
    }
  }
};
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  width: 100%;
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0px;
  color: #fff;
  font-size: 20px;
  .homeLogo {
    display: flex;
    align-items: center;
    img {
      margin-right: 15px;
    }
  }
}
.el-aside {
  background: #363337;
}
.el-menu {
  border-right: none;
}
.iconfont {
  margin-right: 15px;
}
.toggle_button {
  text-align: center;
  line-height: 24px;
  background: #4a5064;
  cursor: pointer;
  letter-spacing: 0.2em;
  color: #fff;
  font-size: 10px;
}
</style>